#include <URGE/URGE.h>
#include "src/Player.h"
#include "src/Powerup.h"
#include "src/Enemy.h"
#include "src/Projectile.h"
#include <time.h>
#include <stdlib.h>

#define TERRAIN 0
#define JUMP_POWERUP 1
#define SPEED_POWERUP 2
#define GUN_POWERUP 3
#define ENEMY_TYPE 4
#define ENEMY 4
#define PLAYER_START_X 10
#define PLAYER_START_Y 10
#define PLAYER_START_Z 10


USING_URGE;

URGE_BEGIN
{

    srand(time(NULL));

    //new window:RX, RY, FPS, PARAMS,  WINDOW TITTLE,      RENDER QUALITY LEVEL
    gimme_window(1280,1024,60, FULLSCREEN, "URGE Application", RENDER_QUALITY_HIGH);

    Scenario scene(1000);
    Sky sky;
    sky.loadTexture("content/images/sky.jpg");

    double gameTime = 0.00;



    Terrain gameTerrain;
    gameTerrain.type() = TERRAIN;
    gameTerrain.load("content/images/height2.png", -1, 30, 300, 300, "content/images/heightTex.png");
    gameTerrain.position(0,0,0);

    Enemy enemy(PLAYER_START_X + 20 + rand()%20, PLAYER_START_Y, PLAYER_START_Z + 20 + rand()%20);
    enemy.type() = ENEMY_TYPE;


    ParticleEmitter part;
    part.generate(Particle::GLOW);
    part.setColor(255,255,0);
    part.scale(0.3);
    part.setParticleLifeTime(0.3);


    Projectile projectile(&enemy, &part);
    projectile.sphere();
    projectile.createBoundingVolume(Solid::SPHERE);
    projectile.position(-400,-10,-10);
    projectile.scale(0.03,0.03,0.03);
    projectile.visible() = false;



    Player player(PLAYER_START_X,PLAYER_START_Y,PLAYER_START_Z,100, &projectile);

    Mouse::cursor.masked();
    Mouse::cursor.load("content/images/cross.png");

    Light flashLight;
    flashLight.spot();
    flashLight.setIntensity(30.0);
    flashLight.setColor(255,255,0);

    Light light;
    light.directional();


    //(x,y,z,scaleX,scaleY,scaleZ,const char *path,type['b' | 's'],dynamic)
    Powerup speedPUP(PLAYER_START_X - 50 + rand()%100, PLAYER_START_Y, PLAYER_START_Z - 50 + rand()%100 ,6,6,6,"content/images/tenis1.png",'b',true);
    speedPUP.type() = SPEED_POWERUP;

    Powerup jumpPUP(PLAYER_START_X + 70 + rand()%140, PLAYER_START_Y, PLAYER_START_Z + 70 + rand()%140 , 6, 6, 6, "content/images/tenis2.png", 'b',true);
    jumpPUP.type() = JUMP_POWERUP;

    Powerup gunPUP(PLAYER_START_X + 66 - rand()%132, PLAYER_START_Y, PLAYER_START_Z + 66 + rand()%132 , 6, 6, 6, "content/images/magic.png", 'b',true);
    gunPUP.type() = GUN_POWERUP;



    scene.insert(&sky);
    scene.insert(&gameTerrain);
    scene.insert(&player);
    scene.insert(&flashLight);
    scene.insert(&speedPUP);
    scene.insert(&jumpPUP);
    scene.insert(&gunPUP);
    scene.insert(&enemy);
    scene.insert(&projectile);
    scene.insert(&light);
    scene.insert(&part);


    scene.prepare();

    //Mouse::blocked() = true;
    Mouse::blocked() = false;

    do
    {

        //printf("Rand: %d\n", rand());

        if (Keyboard::hit(KEYBOARD_ESC))  break;

        if (Mouse::hit(MOUSE_LEFT) && player.getHoldsGun())
        {
           //Text::write(400,300,"  X:%d,  Y:%d",Mouse::x(),Mouse::y());
            part.start();
            projectile.activeBody();
            projectile.body->gravityScale() = 0.001;
            projectile.visible() = true;
            part.visible() = true;
            projectile.position() = player.position();
            projectile.body->vel = (Mouse::ray() * 10);


        }
        part.position() = projectile.position();
        //enemy movement is controlled here
        //DESATIVADO POR EQTO
        Array pos_nova_enemy;


         if(player.checkGameOver())
        {
            pos_nova_enemy.setDirection(enemy.position().getx(), enemy.position().gety(), enemy.position().getz(), 0, 0, 0, 0.1);
            pos_nova_enemy += enemy.position();
            player.dropGun();
            player.inactiveBody();
            Text::writeCenter(400, 300, "Game Over!");
            Text::writeCenter(400, 320, "Score: %d",player.projectile->nFrags);


            //break;
        }else
        {
            pos_nova_enemy.setDirection(enemy.position().getx(), enemy.position().gety(), enemy.position().getz(), player.position().getx(), player.position().gety(), player.position().getz(), 0.15);
            pos_nova_enemy += enemy.position();
        }

        enemy.position(pos_nova_enemy);
        Math::Array direction;

        direction.setDirection(enemy.position().getx(), enemy.position().gety(), enemy.position().getz(), player.position().getx(), player.position().gety(), player.position().getz(), 1.0);
        enemy.body->dir.set(direction.getx(),direction.gety(),direction.getz());

        flashLight.position() = player.position();
        flashLight.direction(Mouse::ray());


        gameTime+= 0.01;
        light.direction(cos(gameTime),sin(gameTime),0);

        if (abs(cos(gameTime)) > 0.9999)
        {
            if (sin(gameTime) < 0)
            {
                   sky.loadTexture("content/images/night.jpg");
            }else{
                   sky.loadTexture("content/images/Sky.jpg");
            }
        }



        Mouse::render();
        scene.update();




        next_frame();


    }while(true);


    thanks_byebye();
}
URGE_END

